
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
import java.nio.charset.CharsetEncoder;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import com.sun.nio.sctp.MessageInfo;
import com.sun.nio.sctp.SctpChannel;
import com.sun.nio.sctp.SctpServerChannel;

public class MultiStreamServer {
	private static final int BUFFER_SIZE = 1024;
	static int SERVER_PORT = 3456;

	public static void main(String[] args) throws IOException {
		SctpServerChannel ssc = SctpServerChannel.open();
		InetSocketAddress serverAddr = new InetSocketAddress(SERVER_PORT);
		ssc.bind(serverAddr);

		ByteBuffer buf =  null;
		byte[] buffArray = new byte[BUFFER_SIZE];

		FileInputStream in = null;

		BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
		String inStr;

		SctpChannel sc;

		CharBuffer cbuf = CharBuffer.allocate(60);
		Charset charset = Charset.forName("ISO-8859-1");
		CharsetEncoder encoder = charset.newEncoder();


		if((sc = ssc.accept())!=null)
		{
			printInfo(sc);
		}
		else
		{
			System.out.println("Something is up when trying to connect!\n");
		}

		/* get the current date
            Date today = new Date(); */

		MessageInfo messageInfo = MessageInfo.createOutgoing(null,0);


		try {
			in = new FileInputStream("wallpaper-nuke.jpg");

			int c;
			int offset = 0;

			while ((c = in.read( buffArray, 0, BUFFER_SIZE ) ) != -1) {
				offset = offset + c;

				System.out.println( "read " + c + "bytes, offset=" + offset);

				//buf.put(buffArray);
				//buf.limit(c);
				buf = ByteBuffer.wrap(buffArray);
				buf.limit(c);
				sc.send( buf , messageInfo);

				buf.clear();	
			}

			while(true) {
				char[] today=new char[1024];

				inStr= stdIn.readLine();
				today=inStr.toCharArray();
				cbuf.put(today).flip();
				encoder.encode(cbuf, buf, true);
				buf.flip();
				System.out.println(buf.toString());

				messageInfo.streamNumber(1);
				sc.send(buf, messageInfo);

				cbuf.clear();
				buf.clear();
			}

		} finally {
			if (in != null) {
				in.close();
			}
			

			sc.close();

		}

		//buf.clear();


	}


	public static void printInfo(SctpChannel sctpChannel)
	{
		try
		{
			System.out.println("sctpChannel.getRemoteAddresses() = " + sctpChannel.getRemoteAddresses());
			System.out.println("sctpChannel.getAllLocalAddresses() = " + sctpChannel.getAllLocalAddresses());
			System.out.println("sctpChannel.isConnectionPending() = " + sctpChannel.isConnectionPending());
			System.out.println("sctpChannel.isOpen() = " + sctpChannel.isOpen());
			System.out.println("sctpChannel.isRegistered() = " + sctpChannel.isRegistered());
			System.out.println("sctpChannel.provider() = " + sctpChannel.provider());
			System.out.println("sctpChannel.association() = " + sctpChannel.association());
		}
		catch(IOException e)
		{
			e.printStackTrace();
		}
	}
}
